package com.shuqi.activity.adapter;

import java.util.List;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.Color;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.shuqi.common.utils.Log4an;
import com.shuqi.common.utils.SqImageManager;
import com.shuqi.controller.R;
import com.shuqi.model.bean.OfferWallInfo;

/**
 * 推广墙列表适配器
 * 
 * @author linyuren
 * 
 */
public class OfferWallListAdapter extends BaseAdapter {
    /** from xml to View */
    private LayoutInflater inflater;
    /** 数据集合 */
    private List<OfferWallInfo> data;
    private Context context;
    private String type;

    public OfferWallListAdapter(Context context) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);

    }

    public void setList(List<OfferWallInfo> data, String type) {
        this.data = data;
        this.type = type;
    }

    @Override
    public int getCount() {
        return data == null ? 0 : data.size();
    }

    @Override
    public Object getItem(int position) {
        return data == null ? null : data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    private String update = "更新"; // Tim
    private String free = "下载";// Tim

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // 获取当前Item
        View view = null;
        if (convertView != null) {
            view = convertView;
        } else {
            view = inflater.inflate(R.layout.itemlayout_offerwall_list, parent, false);
        }
        // 保存当前Item中控件的引用, 每个View对象里面可以通过setTag方法保存一个对象
        Holder holder = (Holder) view.getTag();
        if (holder == null) {
            holder = new Holder();
            holder.iv = (ImageView) view.findViewById(R.id.itemlayout_offerwall_list_iv);
            holder.tv1 = (TextView) view.findViewById(R.id.itemlayout_offerwall_list_tv1);
            holder.tv2 = (TextView) view.findViewById(R.id.itemlayout_offerwall_list_tv2);
            holder.tv3 = (TextView) view.findViewById(R.id.itemlayout_offerwall_list_tv3);
            holder.tv4 = (TextView) view.findViewById(R.id.itemlayout_offerwall_list_tv4);
            view.setTag(holder);
        }
        // 给当前Item中的控件设值
        String imageUrl = data.get(position).getIconUrl();
        SqImageManager.getImageManager().displayImage(imageUrl, holder.iv,
                SqImageManager.IMG_DEFAULT);
        holder.tv1.setText(data.get(position).getName());
        holder.tv2.setText(data.get(position).getIntro());
        holder.tv3.setText(data.get(position).getSize());

        PackageInfo pi = null;
        try {
            pi = context.getPackageManager().getPackageInfo(data.get(position).getPackageName(), 0);
        } catch (NameNotFoundException e) {
            Log4an.e("---lxs.debug.OfferWallListAdapter---", "packageNameNotFoundException");
        }
        if (pi != null) {
            Intent intent = context.getPackageManager().getLaunchIntentForPackage(pi.packageName);
            if (intent != null) {
                try {
                    int versionCode = Integer.valueOf(data.get(position).getVersionCode());
                    if (versionCode > pi.versionCode) {
                        holder.tv4.setText(update);
                        holder.tv4.setBackgroundResource(R.drawable.btn_coomon_green_ok_p);
                        holder.tv4.setTextColor(Color.WHITE);
                    } else {
                        holder.tv4.setText("已装");
                        holder.tv4.setBackgroundDrawable(null);
                        holder.tv4.setTextColor(Color.parseColor("#ffa200"));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                holder.tv4.setText(free);
                holder.tv4.setBackgroundResource(R.drawable.btn_coomon_green_ok_p);
                holder.tv4.setTextColor(Color.WHITE);
            }
        } else {
            holder.tv4.setText(free);
            holder.tv4.setBackgroundResource(R.drawable.btn_coomon_green_ok_p);
            holder.tv4.setTextColor(Color.WHITE);
        }
        holder.tv4.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (update.equals(((TextView) v).getText())) {
                    // Url后拼接site=1表示第一个页面，update=1表示更新下载
                    try {
                        Intent web =
                                new Intent(Intent.ACTION_VIEW, Uri.parse(data.get(position)
                                        .getDownloadUrl()
                                        + "&site="
                                        + ("game".equals(type) ? 3 : 1) + "&update=1"));
                        context.startActivity(web);
                    } catch (Exception e) {
                    }

                } else if (free.equals(((TextView) v).getText())) {
                    // Url后拼接sitek
                    try {
                        Intent web =
                                new Intent(Intent.ACTION_VIEW, Uri.parse(data.get(position)
                                        .getDownloadUrl()
                                        + "&site="
                                        + ("game".equals(type) ? 3 : 1) + "&update=0"));
                        context.startActivity(web);
                    } catch (Exception e) {
                    }

                } else {
                    PackageInfo pi = null;
                    try {
                        pi =
                                context.getPackageManager().getPackageInfo(
                                        data.get(position).getPackageName(), 0);
                    } catch (NameNotFoundException e) {
                        e.printStackTrace();
                    }
                    if (pi != null) {
                        Intent intent =
                                context.getPackageManager().getLaunchIntentForPackage(
                                        pi.packageName);
                        ComponentName cn = intent.getComponent();
                        intent = new Intent(Intent.ACTION_MAIN);
                        intent.setComponent(cn);
                        intent.addCategory(Intent.CATEGORY_LAUNCHER);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        context.startActivity(intent);
                    }
                }
            }
        });
        return view;
    }

    private static class Holder {
        public ImageView iv;
        public TextView tv1;
        public TextView tv2;
        public TextView tv3;
        public TextView tv4;
    }

}
